package com.teach.controller;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.teach.dto.LoginUser;
import com.teach.entity.ShopSlide;
import com.teach.entity.Shops;
import com.teach.service.NewsClassService;
import com.teach.service.NewsService;
import com.teach.service.ShopsService;
import com.teach.service.shop.ShopIndexService;

@Controller
public class IndexController {
	
	@Autowired
	private NewsClassService newsClassService;

	@Autowired
	private NewsClassService ShopsService;
	@Autowired
	private MongoTemplate template;	
	@Autowired
	ShopIndexService sis;
	@Autowired
	private NewsService newsService;

	@RequestMapping("/header.do")
	public String doList(String mb,Model model){
		model.addAttribute("list", newsClassService.listAll());
		if(StringUtils.isEmpty(mb)){
			return "header";
		}else{
			return mb;
		}
	}
	
	@GetMapping("photo-upload.html")
	public String doPhotoUpload(){
		return "photo-upload";
	}
	@GetMapping("/news.html")
	public String doNews() {
		return "news/news-index";
	}
	
	@GetMapping("left-menu.do")
	public String doLeftMenu(LoginUser user,Model model){
		model.addAttribute("user", user);
		return "left-menu";
	}
	
	@GetMapping("user.html")
	public String doUser(){
		return "user";
	}
	
	@GetMapping("manage/")
	public String doManage(){
		return "manage";
	}
	
	
	@RequestMapping("/hotel.do")
	public String doList1(Model model, HttpServletRequest request){
		Query query = new Query();
		query.addCriteria(Criteria.where("shopTypeId").is(1));
		query.addCriteria(Criteria.where("shopStatus").is(2));
		List<Shops> shops = template.find(query, Shops.class);
		Collections.shuffle(shops);
		List<Shops> list1 = shops.subList(0, 4);
		model.addAttribute("list", list1);
		return "hotel-index-3";
	}
	
	@RequestMapping("/lunbo.do")
//	@ResponseBody
	public String doShopLunBo(Model model){
//	public List<ShopSlide> doShopLunBo(Model model){
//		List<ShopSlide> shopSlide = new ArrayList<ShopSlide>();
		List<ShopSlide> shopSlide = sis.slideFindAll();
		Collections.shuffle(shopSlide);
		shopSlide  = shopSlide.subList(0, 5);
		model.addAttribute("shopSlide",shopSlide);
//		return sis.slideFindAll();
		return "shop/shop-lunbo";
	}
	
	@RequestMapping("/news.do")
	public Object doindex(Model model){
		model.addAttribute("news",newsService.indexlist() );
		return "news/new-index-imgs";
	}
	
	@RequestMapping("/food.do")
	public String food(Model model, HttpServletRequest request){
		Query query = new Query();
		query.addCriteria(Criteria.where("shopTypeId").is(0));
		query.addCriteria(Criteria.where("shopStatus").is(2));
		List<Shops> shops = template.find(query, Shops.class);
		List<Shops> lis = shops.subList(3, 7);
		model.addAttribute("list", lis);
		return "food-list";
	}
}
